<?php

class m_harsat extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					t_monitor_id,
					harsat_ket,
					harsat_satuan,
					harsat_nilai,
					harsat_id
				FROM t_harsat
				WHERE 
					1 = 1 
		";
		
		if (@$t_monitor_id != "") $sql .= " AND t_monitor_id like '%".@$t_monitor_id."%'";
		if (@$harsat_ket != "") $sql .= " AND harsat_ket like '%".@$harsat_ket."%'";
		if (@$harsat_satuan != "") $sql .= " AND harsat_satuan like '%".@$harsat_satuan."%'";
		if (@$harsat_nilai != "") $sql .= " AND harsat_nilai like '%".@$harsat_nilai."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows)." , ".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('t_harsat',$data);
		return 1;
	}
	
	function _update($data,$id){
		$this->db->where('harsat_id',$id);
		$this->db->update('t_harsat',$data);
		return 1;
	}
	
	function _delete($id){
		$this->db->where('harsat_id',$id);
		$this->db->delete('t_harsat');
		return 1;
	}
	
	function _deletebymonitor($id){
		$this->db->where('t_monitor_id',$id);
		$this->db->delete('t_harsat');
		return 1;
	}
	
    function _listbymonitor($id){
		$this->db->where('t_monitor_id',$id);
		$query = $this->db->get('t_harsat');
        $result = $query->result();
		return $result;
    }
    
	function _deactivate($data,$id){
		$this->db->where('harsat_id',$id);
		$this->db->update('t_harsat',$data);
		return 1;
	}
}